﻿using MySql.Data.MySqlClient;
using studentskicentarETFa;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for WindowKoordinatorZaInstrukcije.xaml
    /// </summary>
    public partial class WindowKoordinatorZaInstrukcije : Window
    {
        
        public WindowKoordinatorZaInstrukcije()
        {
            InitializeComponent();
        

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DodavanjePredmeta d = new DodavanjePredmeta();
            d.Show();
        }

        //pregled instruktora u gridu
        private void PregledInstruktoraGrud_Loaded(object sender, RoutedEventArgs e)
        {
            /*studentskicentarETFa.DAL d = studentskicentarETFa.DAL.Instanca;
            d.kreirajKonekciju("localhost", "Centar", "root", "");
            studentskicentarETFa.DAL.ZaposlenikDAO z = d.getDAO.getZaposlenikDAO();
            List<Zaposlenik> p = new List<Zaposlenik>();
            p = z.getByExample2("Instruktor");
            try
            {
                
                PregledInstruktoraGrud.ItemsSource = p;
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }*/
            BindMyDataInstruktor();
        }

        //pregled predmeta u gridu
        private void PregledPredmetaGrid_Loaded(object sender, RoutedEventArgs e)
        {
            MySqlConnection con = null;
            string connectionString = "server=localhost;user=root;pwd=;database=centar";
            con = new MySqlConnection(connectionString);

            try
            {

                con.Open();
                MySqlCommand comm = new MySqlCommand("SELECT * FROM Predmet", con);
                comm.CommandText = "SELECT * from Predmet";
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter(comm);
                da.Fill(ds);
                PregledPredmetaGrid.ItemsSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                con.Close();
            }
        }

      

        private void DodajInstruktora_Click(object sender, RoutedEventArgs e)
        {
            DodavanjeZaposlenika di = new DodavanjeZaposlenika(PregledInstruktoraGrud, "Instruktor");
            di.Show();
        }

        private void Dodavanje_Click(object sender, RoutedEventArgs e)
        {
            DodavanjePredmeta dp = new DodavanjePredmeta(PregledPredmetaGrid);
            dp.Show();
           
        }

        //za brisanje predmeta
        private void BindMyData() {
            MySqlConnection con = null;
            string connectionString = "server=localhost;user=root;pwd=;database=centar"; 
            con = new MySqlConnection(connectionString);
            try
            { 
                con.Open(); 
                MySqlCommand comm = new MySqlCommand("SELECT * FROM Predmet", con); 
                comm.CommandText = "SELECT * from Predmet";
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter(comm); 
                da.Fill(ds); 
                PregledPredmetaGrid.ItemsSource = ds.Tables[0].DefaultView; 
            } 
            catch (Exception ex) 
            {
                
                MessageBox.Show(ex.Message.ToString()); 
            } 
            finally
            { 
                con.Close(); 
            }
        }
        //za brisanje i refresh instruktora
        private void BindMyDataInstruktor()
        {
            MySqlConnection con = null;
            string connectionString = "server=localhost;user=root;pwd=;database=centar";
            con = new MySqlConnection(connectionString);
            try
            {
                con.Open();
                MySqlCommand comm = new MySqlCommand("SELECT * from Zaposlenik where Pozicija=@Pozicija", con);
                comm.Parameters.AddWithValue("@Pozicija", "Instruktor");
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter(comm);
                da.Fill(ds);
                PregledInstruktoraGrud.ItemsSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                con.Close();
            }
        }
        private void ObrisiPredmetButton_Click(object sender, RoutedEventArgs e)
        {
         
         
            try
            {
                string MyConnection2 = "server=localhost;user=root;pwd=;database=Centar";
                string Query = "delete from Predmet where ID_Predmeta='" + this.TextBoxPredmet.Text + "';";
                MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
                MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
                MySqlDataReader MyReader2;
                MyConn2.Open();
                MyReader2 = MyCommand2.ExecuteReader();
                MessageBox.Show("Predmet je obrisan.");
                MyConn2.Close();
                BindMyData();

            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }
           
        }

        private void Obrisi_instruktora_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                string MyConnection3 = "server=localhost;user=root;pwd=;database=Centar";
                string Query = "delete from Zaposlenik where ID_Clana='" + this.TextBoxBrisanjeInstruktor.Text + "';";
                MySqlConnection MyConn3 = new MySqlConnection(MyConnection3);
                MySqlCommand MyCommand3 = new MySqlCommand(Query, MyConn3);
                MySqlDataReader MyReader3;
                MyConn3.Open();
                MyReader3 = MyCommand3.ExecuteReader();
                MessageBox.Show("Uspjesno ste obrisali instruktora.");
                while (MyReader3.Read())
                MyConn3.Close();
                BindMyDataInstruktor();

            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }
        }

        private void TabItem_Loaded(object sender, RoutedEventArgs e)
        {
            webRas.Navigate("https://docs.google.com/document/d/1HQiFTOIgvKAxTeFCgLMQXMIGtiSSfGxaFh5q9Fn0hzg/edit");
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            MainWindow m = new MainWindow();
            m.Show();
            this.Hide();
        }


        private void PregledInstruktoraGrud_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            studentskicentarETFa.DAL d = studentskicentarETFa.DAL.Instanca;
            d.kreirajKonekciju("localhost", "Centar", "root", "");
            studentskicentarETFa.DAL.ZaposlenikDAO z = d.getDAO.getZaposlenikDAO();
            List<Zaposlenik> p = new List<Zaposlenik>();
            p = z.getByExample2("Instruktor");
            TextBoxBrisanjeInstruktor.Text = p[PregledInstruktoraGrud.SelectedIndex].ID_Clana1;
        }

        private void PregledPredmetaGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            studentskicentarETFa.DAL d = studentskicentarETFa.DAL.Instanca;
            d.kreirajKonekciju("localhost", "Centar", "root", "");
            studentskicentarETFa.DAL.PredmetDAO z = d.getDAO.getPredmetDAO();
            List<Predmet> p = new List<Predmet>();
            p = z.getAll();
            TextBoxPredmet.Text = p[PregledPredmetaGrid.SelectedIndex].IDPredmeta1;
        }
    }
}
